package com.jjmmbb.anydolist;

import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.jjmmbb.dolist.ex.DialogLoading;
import com.jjmmbb.dolist.ex.HKDialog;

public class AboutActivity extends BaseActivity implements OnClickListener {
	TextView titleView, aboutView, desVew, rateView, hideFinishView,
			hideTeamView, autoWIFIView;
	Button btBackup, btRestore, btBackup2Text;
	String sp = " : ";
	String version = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about);
		titleView = (TextView) findViewById(R.id.about_title);
		desVew = (TextView) findViewById(R.id.about_tip);
		aboutView = (TextView) findViewById(R.id.about_app_about);
		rateView = (TextView) findViewById(R.id.about_rate);
		hideFinishView = (TextView) findViewById(R.id.about_hideFinish);
		hideTeamView = (TextView) findViewById(R.id.about_hideTeam);
		autoWIFIView = (TextView) findViewById(R.id.about_autoWIFI);

		btBackup = (Button) findViewById(R.id.about_bak);
		btRestore = (Button) findViewById(R.id.about_res);
		btBackup2Text = (Button) findViewById(R.id.about_baktxt);

		rateView.setOnClickListener(this);
		hideFinishView.setOnClickListener(this);
		hideTeamView.setOnClickListener(this);
		autoWIFIView.setOnClickListener(this);
		btBackup.setOnClickListener(this);
		btRestore.setOnClickListener(this);
		btBackup2Text.setOnClickListener(this);
		ini();
	}

	void ini() {

		try {
			version = getPackageManager().getPackageInfo(this.getPackageName(),
					0).versionName;
		} catch (NameNotFoundException e) {
		}
		titleView.setText(getStr(R.string.app_name) + " " + version);

		iniRefresh();
	}

	void iniRefresh() {
		if (Comm.hide_finish) {
			hideFinishView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_on, 0);
		} else {
			hideFinishView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_off, 0);
		}
		if (Comm.hide_team) {
			hideTeamView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_on, 0);
		} else {
			hideTeamView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_off, 0);
		}
		if (Comm.autoWIFI) {
			autoWIFIView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_on, 0);
		} else {
			autoWIFIView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
					R.drawable.icon_off, 0);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about_rate:
			// 显示某个应用的相关信息
			Uri uri = Uri.parse("market://details?id=" + getPackageName());
			Intent it = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(it);
			break;
		case R.id.about_hideFinish:
			Comm.hide_finish = !Comm.hide_finish;
			Util.saveOption2Preferences(this);
			iniRefresh();
			break;
		case R.id.about_hideTeam:
			Comm.hide_team = !Comm.hide_team;
			Util.saveOption2Preferences(this);
			iniRefresh();
			break;
		case R.id.about_autoWIFI:
			Comm.autoWIFI = !Comm.autoWIFI;
			Util.saveOption2Preferences(this);
			if (Comm.autoWIFI) {
				Util.startServer(this);
			}
			iniRefresh();
			break;
		case R.id.about_bak:
			IOType = 1;
			TaskIO task1 = new TaskIO();
			task1.execute();
			break;
		case R.id.about_baktxt:
			IOType = 2;
			TaskIO task2 = new TaskIO();
			task2.execute();
			break;
		case R.id.about_res:
			IOType = 3;
			TaskIO task3 = new TaskIO();
			task3.execute();
			break;
		default:
			break;
		}

	}

	// ----------------备份还原-------------------------------------------

	private int IOType = 1; // 1:备份,2:备份到文本,3:还原
	private boolean isIOSuccess = false;
	private DialogLoading dialogLoading;

	private class TaskIO extends AsyncTask<String, Integer, String> {
		@Override
		protected void onPreExecute() {
			showDialogLoading();
			super.onPreExecute();
		}

		protected String doInBackground(String... params) {
			switch (IOType) {
			case 1:
				isIOSuccess = UtilFile.saveToFile(ap.c.itemManager);
				break;
			case 2:
				isIOSuccess = UtilFile.saveToFile_txt(ap.c.itemManager);
				break;
			case 3:
				isIOSuccess = UtilFile.restoreFromFile(ap.c.itemManager);
				break;
			default:
				break;
			}
			return null;
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			cancelDialogLoading();
			String msg = "";
			if (isIOSuccess) {
				switch (IOType) {
				case 1:
					msg = getString(R.string.bak_success_bak);
					break;
				case 2:
					msg = getString(R.string.bak_success_txt);
					break;
				case 3:
					msg = getString(R.string.bak_success_res);
					break;
				default:
					break;
				}
				HKDialog dialog = new HKDialog(AboutActivity.this, msg);
				dialog.show();
			} else {
				HKDialog dialog = new HKDialog(AboutActivity.this,
						getString(R.string.bak_failure));
				dialog.show();
			}
		}
	}

	void showDialogLoading() {
		dialogLoading = new DialogLoading(this);
		dialogLoading.show();
	}

	void cancelDialogLoading() {
		dialogLoading.hide();
	}

}
